package test;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class SendMessageDeposito {

	private static final String USERNAME = "test1";
	private static final String PASSWORD = "test12341";

	/**
		<solicitudArticulos>
			<idSolicitud></idSolicitud>
			<idModulo></idModulo>
			<articulos>
					<articulo>
						<codigo>String</codigo>
						<cantidad>int</cantidad>
					</articulo>
			<articulos>
		</solicitudArticulos>
	**/
	public static void main(String[] args) {
		String xml = "<solicitudArticulos><idSolicitud>12</idSolicitud><idModulo>12</idModulo>"
			+ "<articulos>"
				+ "<articulo>"
					+ "<codigo>18</codigo>"
					+ "<cantidad>30</cantidad>"
				+ "</articulo>"
				+ "<articulo>"
					+ "<codigo>19</codigo>"
					+ "<cantidad>145</cantidad>"
				+ "</articulo>"
			+ "</articulos>"
		+ "</solicitudArticulos>";
		send(xml);
	}
	
	private static Context getContext()
			throws NamingException {
		Context context = null;
		try {
			Properties jndiProps = new Properties();

			jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jboss.naming.remote.client.InitialContextFactory");

			// URL PROVIDER "remote://localhost:4447"
			String provideURL = "remote://localhost:4447";
			jndiProps.put(Context.PROVIDER_URL,
					System.getProperty(Context.PROVIDER_URL, provideURL));
			jndiProps.put(Context.SECURITY_PRINCIPAL,
					System.getProperty("username", USERNAME));
			jndiProps.put(Context.SECURITY_CREDENTIALS,
					System.getProperty("password", PASSWORD));
			context = new InitialContext(jndiProps);

		} catch (NamingException e) {
			e.printStackTrace();
		}

		return context;
	}
	
	public static void send(String xml) {

		MessageProducer producer = null;
		Session session = null;
		Connection connection = null;
		try {
			Context context = getContext();
			
			ConnectionFactory connectionFactory = (ConnectionFactory) context
					.lookup("jms/RemoteConnectionFactory");

			// Creo la JMS connection, session, producer, and consumer
			connection = connectionFactory.createConnection(USERNAME,
					PASSWORD);

			// creo la session para poder crear el productor con un destino
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

			Destination destination = (Destination) context.lookup("jms/queue/solicitud");

			// creo el productor
			producer = session.createProducer(destination);

			// inicio la conneccion
			connection.start();
			TextMessage textMessage = session.createTextMessage(xml);
			producer.send(textMessage);
			connection.stop();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			try {
				if (producer != null) {
					producer.close();
				}
				if (session != null) {
					session.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
}
